package com.hk.dao.impl;

import com.hk.dao.IProductDao;
import com.hk.model.News;
import com.hk.model.Product;
import com.hk.utils.JdbcMysql;
import com.hk.utils.SqlSymbol;

import java.sql.ResultSet;

/**
 * @Author: zhe
 * @Date: 2021/6/19 12:38
 * @Statement: 产品Dao层接口实现类
 */
public class ProductDaoImpl implements IProductDao {

    //    JDBC连接工具类
    private static JdbcMysql jdbcMysql = JdbcMysql.getInstance();
    private static String sql = "";

    @Override
    public ResultSet findAll(Integer page, Integer limit, Product t) {
        sql = "select * from product where 1=1 ";
        if (!t.getName().equals("")) {
            sql += " and name like "+ SqlSymbol.MARK.getValue()+SqlSymbol.PERCENT.getValue()+t.getName()+SqlSymbol.PERCENT.getValue()+SqlSymbol.MARK.getValue();
        }
        if (t.getPlatformPrice().doubleValue()>=0) {
            sql += " and platform_price between "+
                    SqlSymbol.MARK.getValue()+t.getPlatformPrice().doubleValue()+SqlSymbol.MARK.getValue()+
                    " and "+
                    SqlSymbol.MARK.getValue()+t.getMarketPrice().doubleValue()+SqlSymbol.MARK.getValue();
        }
        if (t.gettMenuId() > 0) {
            sql += " and (menu_id = "+t.gettMenuId()+" or "+" tmenu_id = "+t.gettMenuId()+SqlSymbol.RBRACKET.getValue();
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet count(Integer page, Integer limit, Product t) {
        sql = "select count(1) from product where 1=1 ";
        if (!t.getName().equals("")) {
            sql += " and name like "+ SqlSymbol.MARK.getValue()+SqlSymbol.PERCENT.getValue()+t.getName()+SqlSymbol.PERCENT.getValue()+SqlSymbol.MARK.getValue();
        }
        if (t.getPlatformPrice().doubleValue()>=0) {
            sql += " and platform_price between "+
                    SqlSymbol.MARK.getValue()+t.getPlatformPrice().doubleValue()+SqlSymbol.MARK.getValue()+
                    " and "+
                    SqlSymbol.MARK.getValue()+t.getMarketPrice().doubleValue()+SqlSymbol.MARK.getValue();
        }
        if (t.gettMenuId() > 0) {
            sql += " and (menu_id = "+t.gettMenuId()+" or "+" tmenu_id = "+t.gettMenuId()+SqlSymbol.RBRACKET.getValue();
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findById(Integer id) {
        sql = "select * from product where id = "+id;
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public int save(Product t) {
        sql = "insert into product value " +
                "("+null+ SqlSymbol.COMMA.getValue()+SqlSymbol.MARK.getValue()+t.getImg()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getName()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getDetail()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                t.getMarketPrice()+SqlSymbol.COMMA.getValue()+
                t.getPlatformPrice()+SqlSymbol.COMMA.getValue()+
                t.gettMenuId()+SqlSymbol.COMMA.getValue()+
                t.getMenuId()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtCreate()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                SqlSymbol.RBRACKET.getValue();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int update(Product t) {
        sql = "update product set " +
                "img = "+SqlSymbol.MARK.getValue()+t.getImg()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "name = "+SqlSymbol.MARK.getValue()+t.getName()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "detail = "+SqlSymbol.MARK.getValue()+t.getDetail()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "market_price = "+t.getMarketPrice()+SqlSymbol.COMMA.getValue()+
                "platform_price = "+t.getPlatformPrice()+SqlSymbol.COMMA.getValue()+
                "tmenu_id = "+t.gettMenuId()+SqlSymbol.COMMA.getValue()+
                "menu_id = "+t.getMenuId()+SqlSymbol.COMMA.getValue()+
                "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                " where id = "+t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int delete(Integer id) {
        sql = "delete from product where id = "+id;
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

}
